import { createRouter, createWebHistory } from 'vue-router';
import { useAdminInfoStore } from '../store';
import { ElMessage } from 'element-plus';

const routes = [
    {
        path: '/',
        redirect: '/login',
    },
    {
        path: '/home',
        redirect: '/home/chat',
        name: 'Home',
        component: () => import('../views/home/index.vue'),
        meta: {
            title: '首页',
            requiresAuth: true
        },
        children: [
            {
                path: 'chat',
                name: 'Chat',
                component: () => import('../views/home/components/chat.vue')
            },
            {
                path: 'drawIO',
                name: 'drawIO',
                component: () => import('../views/home/components/drawIO.vue')
            },
            {
                path: 'history',
                name: 'history',
                component: () => import('../views/home/components/history.vue')
            },
            {
                path: 'session',
                name: 'session',
                component: () => import('../views/home/components/session.vue')
            },
            {
                path: 'stream',
                name: 'Stream',
                component: () => import('../views/home/components/stream.vue')
            },
        ],
    },
    {
        path: '/setting',
        redirect: '/setting/knowledgeDoc',
        name: 'Setting',
        component: () => import('../views/setting/index.vue'),
        meta: {
            title: '设置',
            requiresAuth: true
        },
        children: [
            {
                path: 'knowledgeDoc',
                name: '知识文档',
                component: () => import('../views/setting/components/knowledgeDoc.vue')
            },
            {
                path: 'docSplitter',
                name: '文档切片',
                component: () => import('../views/setting/components/docSplitter.vue')
            },
            {
                path: 'splitterDisplay',
                name: '切片展示',
                component: () => import('../views/setting/components/splitterDisplay.vue')
            }
        ],
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/login/index.vue'),
        meta: {
            title: '登录注册'
        }
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

// 全局守卫路由
router.beforeEach((to, from, next) => {
    if (from == null) {
        console.log(111);
    }
    // 显示路由标题
    if (to.meta.title) {
        document.title = to.meta.title as any;
    }
    const resolved = router.resolve(to);
    // 检测路由不存在则拒绝跳转
    if (resolved.matched.length === 0) {
        next(false);
        return;
    }
    const adminStore = useAdminInfoStore();
    // 用户尚未登录
    if (to.meta.requiresAuth && !adminStore.getLoginStatus) {
        ElMessage.error("您的认证信息已过期，请重新登录！");
        next('/login');
    } else {
        next();
    }
});

export default router;    